package com.rd.student.ruanjian.class3.gaochenxi;
import java.util.*;
public class ProjectCollections {
      public static void main(String[] args) throws InterruptedException {
            HashMap map = new HashMap();
//            map.put("1",5);
//            map.put("2",7);
//            map.put("3",6);
//            map.put("4",6);
//            map.put("5",7);
//            map.put("6",6);


//            Object o = map.get("3");
//            System.out.println(o);
//
//            Object o1 = map.get("9");
//            System.out.println(o1);
//            if(o1==null){
//                  int o2=(int) o1;
//                  System.out.println(o2);
//            }


//            Set set=map.keySet();//存放所有的value
//            Iterator it=set.iterator();
//            while(it.hasNext()){
//                  Object key = it.next();//把每个key存储起来
//                  Object value = map.get(key);//根据
//                  System.out.println("<"+key+"->"+value+">");
//            }

            LinkedList list = new LinkedList();
            //预留的一些信息
            Student stu1 = new Student(1, "高晨曦", "软件专业", 3, "nice", 20,876);
            Student stu2 = new Student(2, "张思韬", "软件专业", 3, "nice", 21,562);
            Student stu3 = new Student(3, "姜万里", "计算机专业", 1, "nice", 22,832);
            Student stu4 = new Student(4, "郭佳宝", "计算机专业", 1, "nice", 21,845);
            Student stu5 = new Student(5, "方周", "计算机专业", 1, "nice", 23,972);
            Student stu6 = new Student(6,"孙昊","计算机专业",1,"nice",22,1000);

            list.add(stu1);
            list.add(stu2);
            list.add(stu3);
            list.add(stu4);
            list.add(stu5);
            list.add(stu6);

            Scanner scanner = new Scanner(System.in);
            while (true) {
                  System.out.println("========学生信息管理系统======");
                  System.out.println("0.查看所有小组成员");
                  System.out.println("1.新增小组成员");
                  System.out.println("2.移除小组成员");
                  System.out.println("3.根据班级查询成员列表");
                  System.out.println("4.根据专业查询成员列表");
                  System.out.println("5.根据年龄查询成员列表");
                  System.out.println("6.返回所有成员名字");
                  System.out.println("7.计算成员的平均年龄");
                  System.out.println("8.根据年龄范围返回成员列表:(a).0-18 (b).18-20 (c).20-100");
                  System.out.println("9.退出系统");
                  System.out.println("10.(新增功能)修改学生信息");
                  System.out.println("==========================");
                  switch (scanner.next()) {
                        case "0":
                              System.out.println("<--所有成员信息信息-->");
                              System.out.println(Student.returnList(list));
                              break;
                        case "1":
                              System.out.println("<--新增小组成员-->:");
                              System.out.println(Student.addUtils(list));
                              break;
                        case "2":
                              System.out.println("<--移除小组成员-->");
                              System.out.println(Student.removeUtils(list));
                              break;
                        case "3":
                        case "4":
                        case "5":
                              System.out.println("<--请输入要返回的列表种类:1.班级 2.专业 3.年龄-->");
                              int type = scanner.nextInt();
                              HashMap hashMap = Student.countNum(list, type);
                              Student.info(hashMap);
                              System.out.println("===========");
                              String sna = scanner.next();
                              System.out.println(Student.GetListUtils(list, type, sna));
                              break;
                        case "6":
                              System.out.println("==============");
                              System.out.println("<--返回所有成员的名字-->");
                              System.out.println(Student.returnName(list));
                              break;
                        case "7":
                              System.out.println("<--计算成员的平均年龄-->");
                              System.out.println(Student.AgeUtils(list));
                              break;
                        case "8":
                              System.out.println("<--根据年龄范围返回成员列表-->");
                              System.out.println("请输入一个最小值：");
                              int min = scanner.nextInt();
                              System.out.println("请输入一个最小值：");
                              int max = scanner.nextInt();
                              System.out.println(Student.AgeUtils(min, max, list));
                              break;
                        case "9":
                              System.out.println("loading...");
                              Thread.sleep(1000);
                              System.out.println("欢迎下次使用, 再见!");
                              return;
                        case "10":
                              System.out.println("更改学生信息：");
                              Thread.sleep(1000);
                              System.out.println(Student.change(list));
                        default:
                              System.out.println("指令有误！");
                  }
            }
      }
}
class Student implements Comparable{
      //学号既序号
      private int num;
      //姓名
      public String name;
      //专业
      public String major;
      //班级
      public int cla;
      //评论
      public String desc;
      //年龄
      public int age;

      private int codeName;

      public Student() {

      }

      public Student(String name, String major, int cla) {
            this.name = name;
            this.major = major;
            this.cla = cla;
      }

      public Student(int num, String name, String major, int cla, String desc, int age,int codeName) {
            this.num = num;
            this.name = name;
            this.major = major;
            this.cla = cla;
            this.desc = desc;
            this.age = age;
            this.codeName=codeName;
      }

      public String getName() {
            return name;
      }

      public void setName(String name) {
            this.name = name;
      }

      public String getMajor() {
            return major;
      }

      public void setMajor(String major) {
            this.major = major;
      }

      public String getDesc() {
            return desc;
      }

      public void setDesc(String desc) {
            this.desc = desc;
      }

      public int getAge() {
            return age;
      }

      public void setAge(int age) {
            this.age = age;
      }

      public int getCla() {
            return cla;
      }

      public void setCla(int cla) {
            this.cla = cla;
      }

      public int getCodeName() {
            return codeName;
      }

      public void setCodeName(int codeName) {
            this.codeName = codeName;
      }

      public int getNum() {
            return num;
      }

      public void setNum(int num) {
            this.num = num;
      }


      @Override
      public String toString() {
            return "Student{" +
                    "num=" + num +
                    ", name='" + name + '\'' +
                    ", major='" + major + '\'' +
                    ", cla=" + cla +
                    ", desc='" + desc + '\'' +
                    ", age=" + age +
                    ", codeName=" + codeName +
                    '}';
      }

      //张思韬：第0个
      public static LinkedList returnList(LinkedList list) {
            Student stu;
            LinkedList list1 = new LinkedList();
            for (int i = 0; i < list.size(); i++) {
                  stu = (Student) list.get(i);
                  System.out.println(list.get(i));
            }
            return list;
      }


      //高晨曦 第1个
      //添加学生信息
      //num：为添加学生数量
      public static LinkedList<Student> addUtils(LinkedList<Student> list) {
            int num=0;
            LinkedList list1 = new LinkedList();
            System.out.println("请输入同学信息：");
            Scanner scanner = new Scanner(System.in);
            Student stu =new Student();
            System.out.println("请输入同学的名字：");
            String name = scanner.next();
            stu.name = name;
            for(int j=0;j<list.size();j++){
                  Student student=(Student)list.get(j);
                  if(student.getName().equals(stu.name)){
                        System.out.println("插入元素失败,因为名字和已知list集合中对象的age属性有相同的");
                        return list;
                  }
            }
            //在list的长度基础之上再+i
            // 自动生成学号
            while(num<list.size()){
                  num++;
            }
            stu.num=num+1;

            System.out.println("请输入同学的专业：");
            String major = scanner.next();
            stu.major = major;
            System.out.println("请输入同学的班级：");
            int cla = scanner.nextInt();
            stu.cla = cla;
            System.out.println("请输入同学的年龄：");
            int age = scanner.nextInt();
            stu.age = age;
            System.out.println("请输入同学的描述：");
            String desc = scanner.next();
            stu.desc = desc;
            System.out.println("请输入该同学代码量：");
            int codeName=scanner.nextInt();
            stu.codeName=codeName;
            // 添加到成员列表中
            list1.add(stu);
            list.addAll(list1);
            System.out.println("插入成功");
            return list;//显示所有成员信息
      }

      //高晨曦: 第2个:移除小组成员
      public static LinkedList<Student> removeUtils(LinkedList list) {
            //现在的list的基础上进行删除
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入要移除的成员信息：");
            System.out.print("姓名：");
            String in = scanner.nextLine();
            boolean equ = false;
            Iterator<Student> iterator = list.iterator();
            while (iterator.hasNext()) {
                  Student student = iterator.next();
                  if (student.getName().equals(in)) {
                        equ = true;
                        // 检查班级人数是否为1,如果是1，就不能够删除
                        if (check(student, list)) {
                              System.out.println("班级成员无法删除");
                              return list;
                        }
                        iterator.remove();
                        System.out.println("删除成功：" + student.getName());
                        break;
                  }
            }
            if (!equ) {
                  System.out.println("该成员不存在");
            }
            return list;
      }


      public static HashMap countNum(LinkedList list, int type) {
            HashMap hashMap = new HashMap();
            for (int i = 0; i < list.size(); i++) {
                  Student stu = (Student) list.get(i);
                  String key = "";
                  switch (type) {
                        case 1:
                              key =stu.cla+"";//班级
                              break;
                        case 2:
                              key = stu.major;//专业
                              break;
                        case 3:
                              key = stu.age+"";//年龄
                              break;
                        default:
                              System.out.println("不符合逻辑");
                              return hashMap;
                  }
                  if (!hashMap.containsKey(key)) {
                        //第一次出现 树池话value为1
                        hashMap.put(key, 1);
                  } else {
                        //第二次出现后 获取原来的value+1
                        int value = (int) hashMap.get(key);
                        hashMap.put(key, value + 1);//这个key的下一个值->next
                  }
            }
            return hashMap;//可以传给下一个方法的形参
      }


      //高晨曦：修改学生信息
      public static LinkedList change (LinkedList<Student> list){
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入要修改学生的学号Id");
            int sid = sc.nextInt();
            for(int i=0;i<list.size();i++){
                  Student stu =list.get(i);
                  if(stu.getNum()==sid){
                        System.out.println("请输入要修改的内容");
                        System.out.println("1代码量 ");
                        System.out.println("2 姓名");
                        System.out.println("3 年龄");
                        System.out.println("4 描述");
                        int q = sc.nextInt();
                        System.out.println("请输入修改后内容：");
                        //认识到的问题：输入int 就用nextInt();
                        switch(q){
                              case 1:
                                    int date = sc.nextInt();
                                    stu.setCodeName(date);
                                    System.out.println("修改成功！");
                                    break;
                              case 2:
                                    String date1 = sc.nextLine();
                                    stu.setName(date1);
                                    System.out.println("修改成功！");
                                    break;
                              case 3:
                                    int date2 = sc.nextInt();
                                    stu.setAge(date2);
                                    System.out.println("修改成功！");
                                    break;
                              case 4:
                                    String date3 = sc.nextLine();
                                    stu.setDesc(date3);
                                    System.out.println("修改成功！");
                                    break;
                        }
                        break;
                  }
                  else if (i==list.size()-1)
                  {
                        System.out.println("没有找到该学生，请检查学号是否错误！");
                        change(list);
                  }
            }
            return list;
      }

      //方周：第3，4，5个
      public static LinkedList GetListUtils(LinkedList list, int type, String sna) {
            //根据type来决定用户想要的是班级专业还是年龄
            LinkedList l = new LinkedList();
            for (int i = 0; i < list.size(); i++) {
                  Student stu = (Student) list.get(i);
                  switch (type) {
                        case 1:
                              if (stu.getCla()==Integer.parseInt(sna)) {//班级
                                    l.add(list.get(i));
                              }
                              break;
                        case 2:
                              if (stu.getMajor() == sna) {//姓名
                                    l.add(list.get(i));
                              }
                              break;
                        case 3:
                              if (stu.getAge() ==  Integer.parseInt(sna)) {//年龄
                                    l.add(list.get(i));
                              }
                              break;
                  }
            }
            return l;
      }

      //张思韬：第6个
      public static LinkedList<Student> returnName(LinkedList<Student> list) {
            Student student;
            LinkedList list1 = new LinkedList();
            for (int i = 0; i < list.size(); i++) {
                  student = (Student) (list.get(i));
                  list1.add(student.name);
            }
            return list1;
      }

      /*
            根据年龄查询成员列表(hashMap实现)
                 返回当前所有年龄和年龄对应的人数(HashMap)
              用户可以输入年龄  返回成员列表(LinkedList)
       */

      //高晨曦
      public static boolean check(Student student, List<Student> list) {
            int sum = 0;//一个计数器
            for (Student stu : list) {
                  if (stu.getCla() == student.getCla()) {
                        sum++;
                  }
            }
            return sum == 1;
      }

      //方周：第7个
      public static boolean AgeUtils(LinkedList list) {
            int num = 0;
            for (int i = 0; i < list.size(); i++) {
                  Student stu = (Student) (list.get(i));
                  num += stu.getAge();
            }
            System.out.println("平均年龄为:" + num / list.size());
            Collections.sort(list);
            System.out.print("根据年龄升序排为：");
            System.out.println(list);
            DownSort(list);
            System.out.print("根据年龄降序排为：");
            System.out.println(list);
            return false;
      }


      //方周：第8个
      public static LinkedList AgeUtils(int min, int max, LinkedList list) {
            LinkedList l = new LinkedList();
            for (int i = 0; i < list.size(); i++) {
                  Student stu = (Student) list.get(i);
                  if (stu.getAge() >= min && stu.getAge() <= max) {
                        l.add(list.get(i));
                  }
            }
            return l;
      }

      // 引入的降序排序的算法
      public static void DownSort(LinkedList list) {
            //逆序排序
            Collections.sort(list);
            int i = 0, j = list.size() - 1;
            Object o;
            while (i < j) {
                  o = list.get(j);
                  list.set(j, list.get(i));
                  list.set(i, o);
                  i++;
                  j--;
            }
      }

      public static void info(HashMap hashMap) {
            // 因为所有的key都不能重复  所以key组成了一个set
            Set set = hashMap.keySet();
            /*
                  在 Java 中， HashMap 的 keySet 方法返回所有键的 Set 视图。
                  这意味着，如果要访问 HashMap 中存储的所有键，可以使用 keySet 方法获得这些键的 Set 视图。
                  可以使用该 Set 视图进行遍历、迭代或删除键。
                  其中：
                  ||：如果前一个为真，那么后一个就不会执行了
             */
            Iterator iterator = set.iterator();
            while (iterator.hasNext()) {
                  Object key = iterator.next();
                  Object value = hashMap.get(key);
                  System.out.println("<" + key + " ," + value + "人" + ">");
            }
      }

      @Override
      public int compareTo(Object o) {
            Student stu = (Student) o;
            if (this.age > stu.age) {
                  return 1;
            } else if (this.age < stu.age) {
                  return -1;
            }
            return 0;
      }
}
